/*********************************************************************************************************************/
/*-----------------------------------------------------Includes------------------------------------------------------*/
/*********************************************************************************************************************/
#include "00_APP/UserSW/inc/direction.h"

/*********************************************************************************************************************/
/*-------------------------------------------------Global variables--------------------------------------------------*/
/*********************************************************************************************************************/
UINT_8 ir_val = 0;
UINT_8 state = 0;
float ultrasonic_val;

/*********************************************************************************************************************/
/*---------------------------------------------Function Implementations----------------------------------------------*/
/*********************************************************************************************************************/
/* Main Application */
void line_tracer(void)
{
    ir_val = IR_data();
//    ultrasonic_val = ultrasonic_data();
    ultrasonic_val = 4501;

    /*
     * black line = 0
     * white line = 1
     * sensor   DEC_value   Handle Direction
     * 0000     0          No_line
     * 0011     3          hard_right
     * 0010     2           soft_right
     * 0111     7           right
     *
     * 1010     10           Forward
     *
     * 1000     8           hard_left
     * 1100     12           hard_left
     * 1101     13           soft_left
     *
     */
    if (ultrasonic_val > 4500) {
        switch (ir_val) {
            case 0:
            case 10:
                MT_forward();
                state = ir_val;
                break;
            case 12:
            case 13:
                MT_hard_left();
                state = ir_val;
                break;
            case 3:
            case 7:
                MT_hard_right();
                state = ir_val;
                break;
            default:
                if (state == 0 || state == 10) {
                    MT_forward();
                } else if (state == 12 || state == 13) {
                    MT_hard_left();
                } else if (state == 3 || state == 7) {
                    MT_hard_right();
                }
                break;
        }
    } else {
        MT_stop();
    }
}
